/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.rest;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import pa165.dominating.species.tos.ElementTo;

/**
 *
 * @author Ondrej Prazak
 */
public class ElementTableModel extends AbstractTableModel{
    
    private static enum COLUMNS{
      ID, TYPE
    }
    
    private List<ElementTo> elements = new ArrayList<>();
    private EnumConverter converter = new EnumConverter();

    @Override
    public int getRowCount() {
        return elements.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMNS.values().length;
    }
    

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ElementTo ele = elements.get(rowIndex);
        switch(COLUMNS.values()[columnIndex]){
            case ID: return ele.getId();
            case TYPE: return converter.enum2String(ele.getType());
            default: throw new IllegalArgumentException("column index");
        }
    }
    
    @Override
    public String getColumnName(int columnIndex){
      switch(COLUMNS.values()[columnIndex]){
            case ID: return "ID";
            case TYPE: return "Type";
            default: throw new IllegalArgumentException("column index");
        }
    }
    
    public void addElementToTable(ElementTo ele){
        elements.add(ele);
        int lastRow = elements.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);        
    }
    
    public void deleteElementFromTable(ElementTo ele){
        int index = elements.indexOf(ele);
        elements.remove(ele);
        fireTableRowsDeleted(index, index);
    }
    
    public void updateElementInTable(ElementTo ele){
      int index = elements.indexOf(ele);
      fireTableRowsUpdated(index, index);
    }
    
    public ElementTo getSelectedElement(int rowIndex){
        return elements.get(rowIndex);
    }

    public List<ElementTo> getElements() {
        return elements;
    }
    
    
    
}
